/**
 * 
 */
package com.autonavi.car.si.FileStore.task;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import com.autonavi.car.si.FileStore.model.FileVO;
import com.autonavi.car.si.FileStore.util.ConfigUtil;
import com.autonavi.car.si.FileStore.util.StringUtil;
import com.autonavi.car.si.FileStore.util.ThreadPoolUtil;

/**
 * @author kucheng.liao@autonavi.com
 * @date Nov 24, 2014
 * 
 */
public class FileQueue {

	private static BlockingQueue<FileVO> queue = new LinkedBlockingQueue<FileVO>();

	public static void push(FileVO vo) {
		init();
		if (null != vo) {
			try {
				queue.put(vo);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private static volatile boolean inited = true;

	private static void init() {
		if (inited) {
			synchronized (FileQueue.class) {
				if (inited) {
					String thread_num = ConfigUtil.get("THREAD_NUM");
					int number = 0;
					if (StringUtil.isNotEmpty(thread_num)) {
						number = Integer.parseInt(thread_num);
					} else {
						number = Runtime.getRuntime().availableProcessors();
					}
					while (number > 0) {
						FileTask task = new FileTask(queue);
						ThreadPoolUtil.execute(task);
						number--;
					}
				}
			}
		}
	}

}
